/*----------------------------------------------------------------------------
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in
compliance with the License. You may obtain a copy of the 
License at http://www.mozilla.org/MPL/
	
Software distributed under the License is distributed on 
an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, 
either express or implied. 
See the License for the specific language governing rights and 
limitations under the License.
	
The Original Code is Fever RS ( ActionScript 3 ) Framework code.
	
The Initial Developer of the Original Code is Romain Ecarnot.
Portions created by Initial Developer are Copyright (C) 2006
the Initial Developer. All Rights Reserved.
	
Contributor(s):
	
Unless required by applicable law or agreed to in writing, 
software distributed under the License is distributed on 
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 
OF ANY KIND, either express or implied.
See the License for the specific language governing permissions 
and limitations under the License.
----------------------------------------------------------------------------*/

package fever.data.validators.strategy 
{
	import com.bourre.structures.Range;		import fever.data.validators.ValidatorStrategy;	
	/**
	 * The StringValidatorStrategy class validates that the length of 
	 * a String is within a specified range.
	 * 
	 * <p>More, can check if the string contains or not a defined 
	 * substring.</p> 
	 * 
	 * @see fever.data.validators.Validator
	 * @see fever.data.validators.strategy Fever Implemented strategies
	 * 
	 * @author Romain Ecarnot
	 */
	public class StringValidatorStrategy extends ValidatorStrategy
	{
		//--------------------------------------------------------------------
		// Private properties
		//--------------------------------------------------------------------
		
		private var _upperCheck : Boolean;
		private var _include : String;		private var _exclude : String;
		private var _range : Range;
		
				
		//--------------------------------------------------------------------
		// Public properties
		//--------------------------------------------------------------------		
		
		/**
		 * If <code>includeString</code> property is not <code>null</code>, 
		 * specifies that a value that does not contain this 
		 * <code>includeString</code> string causes a validation error.
		 * 
		 * @default null
		 */
		public function get includeString() : String { return _include; }
		/** @private */
		public function set includeString ( subString : String ) : void { _include = subString;	}
		
		/**
		 * If <code>excludeString</code> property is not <code>null</code>, 
		 * specifies that a value that contains this 
		 * <code>excludeString</code> string causes a validation error.
		 * 
		 * @default null
		 */
		public function get excludeString() : String { return _exclude; }
		/** @private */
		public function set excludeString ( subString : String ) : void { _exclude = subString;	}
		
		/**
		 * Defines the minimum and the maximum length for a valid String.
		 * 
		 * @default new Range( 0, Number.POSITIVE_INFINITY )
		 */
		public function get range() : Range { return _range; }
		/** @private */
		public function set range ( o : Range ) : void { _range = o;	}
		
		/**
		 * Setting to <code>true</code> to process a 
		 * <code>toUppperCase()</code> on source string before validation 
		 * process.
		 * 
		 * <p><u>Note</u> : <code>includeString</code> and <code>excludeString</code> 
		 * are not transformed.</p>
		 * 
		 * @default false
		 */
		public function get uppercaseTest() : Boolean { return _upperCheck; }
		/** @private */
		public function set uppercaseTest ( b : Boolean ) : void { _upperCheck = b;	}
		
		
		//--------------------------------------------------------------------
		// Public API
		//--------------------------------------------------------------------
		
		/**
		 * Constructor.
		 */
		public function StringValidatorStrategy()
		{
			super();
			
			_include = null;			_exclude = null;
			_range = new Range( 0 );			_upperCheck = false;
		}	
		
		/** 
		 * @inheritDoc
		 */
		override public function validate( value : Object ) : void
		{
			super.validate( value );
			
			if( !_isNullValue(value) )
			{
				var o : String = ( value != null ) ? String( value ) : "";
				
				if( required && o.length < 1 ) 
				{
					_sendError( "Required" );
					return;
				}
				
				if( _upperCheck ) 
				{
					o = o.toUpperCase();
				}
				
				if( _include != null && o.indexOf( _include ) < 0 )
					_sendError( "Not contains " + _include );
				
				if( _exclude != null && o.indexOf( _exclude ) > -1 )
					_sendError( "Contains " + _exclude );
				
				if( _range != null && !_range.surround( o.length ) ) 
					_sendError( "Illegal length" );
			}
		}
	}
}
